import pygame

# 卡片精灵组
from role import sprite_group


class Window:
    def __init__(self) -> None:
        pygame.init()
        pygame.font.init()
        pygame.mixer.init()
        pygame.mixer.music.load("./PVZ素材/背景音乐/Look_up_at_the_Sky.mp3")
        pygame.mixer.music.play()
        # 加载背景图

        # 初始化窗口
        pygame.display.set_caption("PlantsVsZombies")
        self.screen = pygame.display.set_mode(flags=pygame.FULLSCREEN)
        pygame.display.toggle_fullscreen()
        # 运动精灵组
        self.player = sprite_group.spriteGroup()

        # 帧率控制对象
        self.clock = pygame.time.Clock()

    def CreateObj(self, *sprites):
        """创建卡片精灵组"""
        self.player.add(*sprites)

    def main(self):
        """主程序"""
        runing = True
        xiaojiangshi = sprite_group.ordinary_zombie(x=600, y=320)
        xiaojiangshi1 = sprite_group.ordinary_zombie(x=700, y=420)
        xiaojiangshi2 = sprite_group.ordinary_zombie(x=500, y=520)
        xiaojiangshi3 = sprite_group.ordinary_zombie(x=1000, y=620)

        wandousheshou = sprite_group.peashooter_plant(
            x=280, y=320, sprite_group=self.player
        )
        wandousheshou1 = sprite_group.peashooter_plant(
            x=280, y=420, sprite_group=self.player
        )
        wandousheshou2 = sprite_group.peashooter_plant(
            x=280, y=520, sprite_group=self.player
        )
        wandousheshou3 = sprite_group.peashooter_plant(
            x=280, y=620, sprite_group=self.player
        )
        wandousheshoucard = sprite_group.pea_shooter_card(initSchedule=0)
        self.CreateObj(
            wandousheshou,
            wandousheshou1,
            wandousheshou2,
            wandousheshou3,
            xiaojiangshi,
            xiaojiangshi1,
            xiaojiangshi2,
            xiaojiangshi3,
            wandousheshoucard,
        )
        while runing:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    runing = False
                    pygame.quit()
            # ------------------------------------ 更新 ------------------------------------ #

            # 更新精灵组
            self.player.update()
            # 绘制所有精灵
            self.screen.fill((0, 0, 0))

            self.player.draw(self.screen)
            pygame.transform.smoothscale(
                self.screen, (self.screen.get_width(), self.screen.get_height())
            )
            pygame.display.update()
            self.clock.tick(60)


if __name__ == "__main__":
    window = Window()
    window.main()
